use ark_ff::Field;
use ark_ff::One;
use ark_relations::{
    lc,
    r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError},
};
use ark_relations::r1cs::Variable;
use ark_bn254::Fr;
use ark_relations::r1cs::LinearCombination; 
pub struct AgeRangeCircuit {
    pub birth_year: Fr,
    pub min_age: Fr,
    pub max_age: Fr,
    pub current_year: Fr,
}


impl ConstraintSynthesizer<Fr> for AgeRangeCircuit {
    fn generate_constraints(self, cs: ConstraintSystemRef<Fr>) -> Result<(), SynthesisError> {
        // 计算年龄: current_year - birth_year
        let age = cs.new_witness_variable(|| Ok(self.current_year - self.birth_year))?;
        let min_age_var = cs.new_input_variable(|| Ok(self.min_age))?;
        let max_age_var = cs.new_input_variable(|| Ok(self.max_age))?; 
        // 约束1: age >= min_age
        /* cs.enforce_constraint(
            lc!() + age,
            lc!() + Fr::one(),
            lc!() + self.min_age,
        )?; */
        cs.enforce_constraint(
            lc!() + age,
            lc!() + Variable::One,
            lc!() + min_age_var,
        )?;
        
        // 约束2: age <= max_age
         cs.enforce_constraint(
            lc!() + max_age_var,
            lc!() +Variable::One,
            lc!() + age,
        )?; 
        
        
        
        Ok(())
    }
}  